package com.zhss.eshop.wms.stock;

import com.zhss.eshop.common.util.ObjectUtils;
import com.zhss.eshop.schedule.domain.SaleDeliveryScheduleResult;
import com.zhss.eshop.schedule.domain.dto.ScheduleOrderPickingItemDTO;
import com.zhss.eshop.schedule.service.ScheduleService;
import com.zhss.eshop.wms.domain.dto.WmsGoodsAllocationStockDetailDTO;
import com.zhss.eshop.wms.domain.dto.WmsReturnGoodsInputOrderDTO;
import com.zhss.eshop.wms.domain.dto.WmsReturnGoodsInputOrderItemDTO;
import com.zhss.eshop.wms.domain.dto.WmsReturnGoodsInputOrderPutOnItemDTO;
import com.zhss.eshop.wms.domain.model.WmsGoodsAllocationStock;
import com.zhss.eshop.wms.domain.model.WmsGoodsAllocationStockDetail;
import com.zhss.eshop.wms.domain.model.WmsGoodsStock;
import com.zhss.eshop.wms.mapper.WmsGoodsAllocationStockDetailMapper;
import com.zhss.eshop.wms.mapper.WmsGoodsAllocationStockMapper;
import com.zhss.eshop.wms.mapper.WmsGoodsStockMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 退货入库库存更新组件
 * @author suyuan
 *
 */
@Component
@Scope("prototype")
public class ReturnGoodsInputWmsStockUpdater extends AbstractWmsStockUpdater {

	/**
	 * 退货入库单
	 */
	private WmsReturnGoodsInputOrderDTO returnGoodsInputOrder;
	/**
	 * 商品库存管理的DAO组件
	 */
	@Autowired
	private WmsGoodsStockMapper goodsStockDAO;
	/**
	 * 货位库存管理的DAO组件
	 */
	@Autowired
	private WmsGoodsAllocationStockMapper goodsAllocationStockDAO;
	/**
	 * 货位库存明细管理的DAO组件
	 */
	@Autowired
	private WmsGoodsAllocationStockDetailMapper stockDetailDAO;
	/**
	 * 调度中心接口
	 */
	@Autowired
	private ScheduleService scheduleService;

	/**
	 * 更新商品库存
	 */
	@Override
	protected void updateGoodsStock() throws Exception {
		List<WmsReturnGoodsInputOrderItemDTO> returnGoodsInputOrderItems =
				returnGoodsInputOrder.getItems();

		for(WmsReturnGoodsInputOrderItemDTO returnGoodsInputOrderItem : returnGoodsInputOrderItems) {
			WmsGoodsStock goodsStock = goodsStockDAO.getBySkuId(
					returnGoodsInputOrderItem.getGoodsSkuId());

			Long availableStockQuantity = goodsStock.getAvailableStockQuantity()
					+ returnGoodsInputOrderItem.getArrivalCount();
			goodsStock.setAvailableStockQuantity(availableStockQuantity);

			Long outputStockQuantity = goodsStock.getOutputStockQuantity()
					- returnGoodsInputOrderItem.getArrivalCount();
			goodsStock.setOutputStockQuantity(outputStockQuantity);

			goodsStockDAO.updateByPrimaryKeySelective(goodsStock);
		}
	}

	/**
	 * 更新货位库存
	 */
	@Override
	protected void updateGoodsAllocationStock() throws Exception {
		List<WmsReturnGoodsInputOrderItemDTO> items = returnGoodsInputOrder.getItems();

		for(WmsReturnGoodsInputOrderItemDTO item : items) {
			SaleDeliveryScheduleResult scheduleResult = scheduleService.getScheduleResult(
					returnGoodsInputOrder.getOrderId(), item.getGoodsSkuId());

			for(ScheduleOrderPickingItemDTO pickingItem : scheduleResult.getPickingItems()) {
				WmsGoodsAllocationStock goodsAllocationStock = goodsAllocationStockDAO
						.getBySkuId(pickingItem.getGoodsAllocationId(), pickingItem.getGoodsSkuId());

				Long availableStockQuantity = goodsAllocationStock.getAvailableStockQuantity()
						+ pickingItem.getPickingCount();
				goodsAllocationStock.setAvailableStockQuantity(availableStockQuantity);

				Long outputStockQuantity = goodsAllocationStock.getOutputStockQuantity()
						- pickingItem.getPickingCount();
				goodsAllocationStock.setOutputStockQuantity(outputStockQuantity);

				goodsAllocationStockDAO.updateByPrimaryKeySelective(goodsAllocationStock);
			}
		}
	}

	/**
	 * 更新货位库存明细
	 */
	@Override
	protected void updateGoodsAllocationStockDetail() throws Exception {
		List<WmsReturnGoodsInputOrderItemDTO> items = returnGoodsInputOrder.getItems();

		for(WmsReturnGoodsInputOrderItemDTO item : items) {
			List<WmsReturnGoodsInputOrderPutOnItemDTO> putOnItems = item.getPutOnItems();
			List<WmsGoodsAllocationStockDetail> stockDetails = new ArrayList<>();

			for(WmsReturnGoodsInputOrderPutOnItemDTO putOnItem : putOnItems) {
				WmsGoodsAllocationStockDetail stockDetail = new WmsGoodsAllocationStockDetail();
				stockDetail.setGoodsAllocationId(putOnItem.getGoodsAllocationId());
				stockDetail.setGoodsSkuId(putOnItem.getGoodsSkuId());
				stockDetail.setPutOnQuantity(putOnItem.getPutOnShelvesCount());
				stockDetail.setPutOnTime(putOnItem.getGmtCreate());
				stockDetail.setCurrentStockQuantity(stockDetail.getPutOnQuantity());
				stockDetail.setLockedStockQuantity(0L);

				stockDetailDAO.insertSelective(stockDetail);

				stockDetails.add(stockDetail);
			}

			item.setStockDetails(ObjectUtils.convertList(stockDetails,
					WmsGoodsAllocationStockDetailDTO.class));
		}
 	}

	/**
	 * 设置需要的参数
	 */
	@Override
	public void setParameter(Object parameter) {
		this.returnGoodsInputOrder = (WmsReturnGoodsInputOrderDTO) parameter;
	}

}
